﻿using System.Collections.Generic;
using System.Reflection;

namespace Validator
{
    public class ValidationEngine
    {
        public List<ValidationErrors> Validate(object item)
        {
            List<ValidationErrors> errorList = new List<ValidationErrors>();
            foreach (PropertyInfo propInfo in item.GetType().GetProperties())
            {
                foreach (BaseValidator attr in propInfo.GetCustomAttributes(typeof(BaseValidator), true))
                {
                    if (!attr.IsValid(propInfo.GetValue(item, null)))
                        errorList.Add(new ValidationErrors
                        {
                            ClassName = item.GetType().Name.ToString(),
                            PropertyName = propInfo.Name.ToString(),
                            FriendlyName = attr.FriendlyName,
                            ErrorMessage = attr.Message
                        });
                }
            }

            return errorList;
        }
    }
}
